package com.shuai.frame.utils;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.setting.dialect.Props;
import lombok.extern.slf4j.Slf4j;

/**
 * 加载classPath下的rpc-frame配置文件到配置对象RpcConfig中
 */
@Slf4j
public class LoadConfigUtils {
    private LoadConfigUtils() {
    }

    public static <T> T loadConfigToBean(Class<T> bean, String prefix) {
        return loadConfigToBean(bean, prefix, "");
    }

    public static <T> T loadConfigToBean(Class<T> bean, String prefix, String environment) {
        StringBuilder application = new StringBuilder("application");
        if (CharSequenceUtil.isNotEmpty(environment)) {
            application.append("-").append(environment);
        }
        application.append(".properties");
        log.info("加载了配置文件：{}中的配置项到：{}", application, bean.getName());
        Props properties = Props.getProp(application.toString());
        return properties.toBean(bean, prefix);
    }
}
